package net;

import net.piggy.common.util.general.ApplicationContextUtil;
import net.piggy.model.dto.Query;
import net.piggy.web.mapper.SecurityMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ResourceBanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.ClassPathResource;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@MapperScan("net.piggy.web.mapper")
@SpringBootApplication
public class SecurityApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(SecurityApplication.class);
        springApplication.setBanner(new ResourceBanner(new ClassPathResource("banner.txt")));
        ConfigurableApplicationContext context = springApplication.run(args);

        Method method = ApplicationContextUtil.class.getDeclaredMethod(ApplicationContextUtil.method, ApplicationContext.class);
        method.setAccessible(true);
        method.invoke(null, context);

        SqlSessionFactory factory = context.getBean(SqlSessionFactory.class);
        SqlSession sqlSession = factory.openSession();
        SecurityMapper mapper = sqlSession.getMapper(SecurityMapper.class);
        Query query = new Query();
        query.setRoleId(1);
        List<Map<String, Object>> list = mapper.selectAuthorities(query);
        query.setData(list);
        System.out.println(query);
        sqlSession.close();

    }

}
